package com.leyou.test1.demo;

import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

public class Demo2 {

    private static RestTemplate restTemplate;
    @Autowired
    public Demo2(RestTemplate restTemplate){
        Demo2.restTemplate=restTemplate;
    }

    public static void main(String[] args) {
//也可以使用pojo提交,属性对应的上即可

        //提交表单数据使用
       MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
       map.add("aaa","bbb");
       //往请求头中 添加数据
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("token","dfhdjfhjdfhd");
        //设置请求头
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        //将参数和请求头信息封装进去
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(map,httpHeaders);
        //restTemplate 多种API方法
        User user = restTemplate.postForObject("url", requestEntity, User.class);
    }
    @Data
    class User{
        String username;
        Integer age;
    }
}
